package net.tsz.afinal.download;

/**
 * Created by Jaelyn on 15/11/4.
 */
public class ProgressGet100Util {
    public ProgressGet100Util() {
    }

    public static String get100Progress(long currentPosition, long totalPosition) {
        return (int)((double)(currentPosition / totalPosition) * 100.0D) + "%";
    }

    public static String get100_0Progress(long currentPosition, long totalPosition) {
        return get100_n_0Progress(currentPosition, totalPosition, 1);
    }

    public static String get100_00Progress(long currentPosition, long totalPosition) {
        return get100_n_0Progress(currentPosition, totalPosition, 2);
    }

    public static String get100_000Progress(long currentPosition, long totalPosition) {
        return get100_n_0Progress(currentPosition, totalPosition, 3);
    }

    public static String get100_0000Progress(long currentPosition, long totalPosition) {
        return get100_n_0Progress(currentPosition, totalPosition, 4);
    }

    public static String get100_n_0Progress(long currentPosition, long totalPosition, int n) {
        if(n == 0) {
            return get100Progress(currentPosition, totalPosition);
        } else {
            String p = String.valueOf((double)currentPosition / (double)totalPosition * 100.0D);
            int index = p.indexOf(".") + 1;
            if(index == 0) {
                p = strEndAdd0(p + ".", n);
            } else if(index + n >= p.length()) {
                p = strEndAdd0(p, index + n - p.length());
            } else {
                p = p.substring(0, index + n);
            }

            return p + "%";
        }
    }

    private static String strEndAdd0(String str, int i) {
        for(int j = 0; j < i; ++j) {
            str = str + "0";
        }

        return str;
    }
}
